local skel = fk.CreateSkill {
  name = "emo__jiying",
}

local U = require "packages.utility.utility"

Fk:loadTranslationTable{
  ["emo__jiying"] = "疾影",
  [":emo__jiying"] = "当你使用牌指定唯一目标后，你可获得X张【影】（X为其至你上一张牌唯一目标的距离），<a href='emo__jiying_href'>若不能获得</a>则改为可重铸一张牌。",

  ["emo__jiying_href"] = "不能获得【影】的情况包含：上一张牌目标不唯一；上一张牌的目标与当前使用的牌目标相同；上一张牌目标死亡或调离；当前目标调离",
  ["#emo__jiying-shade"] = "疾影：你可以获得 %arg 张【影】",
  ["#emo__jiying-recast"] = "疾影：你可以重铸一张牌",

  ["$emo__jiying1"] = "",
  ["$emo__jiying2"] = "",
}

skel:addEffect(fk.TargetSpecified, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and data:isOnlyTarget(data.to)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local current = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if current == nil then return false end
    local last = room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
      return e.data.from == player and e.id < current.id
    end, 1)[1]
    if last == nil then return false end -- 没有上一张牌不许触发哈
    local x = 0
    local last_tos = last.data.tos
    if #last_tos == 1 then
      local to = last_tos[1]
      x = data.to:distanceTo(to)  -- 死亡/调离距离为-1，自己到自己距离为0
    end
    if x > 0 then
      if room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__jiying-shade:::"..x }) then
        event:setCostData(self, {x = x})
        return true
      end
    else
      local cards = room:askToCards(player, {min_num = 1, max_num = 1, include_equip = true, skill_name = skel.name, prompt = "#emo__jiying-recast" })
      if #cards > 0 then
        event:setCostData(self, { cards = cards })
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local x = event:getCostData(self).x
    if x then
      room:obtainCard(player, U.getShade(room, x), true, fk.ReasonPrey, player, skel.name)
    else
      room:recastCard(event:getCostData(self).cards, player, skel.name)
    end
  end,
})

return skel
